//
//  FounctionBaseCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/9.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
import SnapKit

fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


enum inputViewState {
    case activated //激活状态
    case disabled //不可用状态
    case wrong //错误状态
}
//protocol inputViewStateProtocol:NSObjectProtocol { //需要扫码输入的cell需遵守此协议
//    var state: inputViewState? {get set}
//    var delegate: FounctionBaseCellInputDelegate? {set get}
//    func finishScanBarcode()
//}

protocol inputViewProtocol:NSObjectProtocol { //需要扫码输入的cell需遵守此协议
    var state: inputViewState? {get set}
    var delegate: normalInputViewStateProtocol? {set get}
    func finishInput(_ text:String?)
}

protocol normalInputViewStateProtocol:NSObjectProtocol { //手动输入的cell需遵守此协议
    func updateDetail(_ value: String , forKey: String)
    func finishUpdateDetail(_ cell: FounctionBaseCell)
    func finishInput(_ cell: FounctionBaseCell)

}

protocol displayDetailProtocol:NSObjectProtocol { //需要展示网络数据的cell需遵守此协议
    var keyToDisplay: String?{set  get}
    func setDetail(_ string: String)
    func isEmpty() -> (Bool)
}

protocol FounctionBaseCellInputDelegate:NSObjectProtocol {
    func finishInput(_ cell: FounctionBaseCell, text: String)
}

class FounctionBaseCell: UITableViewCell {
    //MARK: - property
    lazy var iconView:UIImageView = {
        let icon = UIImageView()
//        icon.backgroundColor = UIColor.yellowColor()
        return icon
    }()
    lazy var titleView:UILabel = {
        let titleView = UILabel()
        if titleView.text?.characters.count > 5{
            titleView.font = UIFont.boldSystemFont(ofSize: 12)
        }
        titleView.font = UIFont.systemFont(ofSize: self.getFont())
        titleView.textColor = UIColor.hexStringToUIColor("#333333")
        titleView.adjustsFontSizeToFitWidth = true
//        titleView.backgroundColor = UIColor.redColor()
        return titleView
    }()
    lazy var separatorView:UIView = {
        let view = UIView(frame: CGRect(x: 13, y: self.height - 0.5, width: KscreenWidth - 26, height: 0))
        view.backgroundColor = UIColor.hexStringToUIColor("#cccccc")
//        view.backgroundColor = UIColor.blueColor()
        return view
    }()

    
    var info:[String : String]?{
        didSet{
            if info![FounctionCellsModel.imageName] != "" && info![FounctionCellsModel.imageName] != nil{
                self.iconView.image = UIImage(named: info![FounctionCellsModel.imageName]!)
            }
            self.titleView.text = info![FounctionCellsModel.title] ?? ""
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUIBase()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUIBase()
    }
    
    required init(){
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUIBase()
    }

    //MARK: - private
    func setUpUIBase(){
        //添加自定义imageView
        //添加自定义textView
        self.contentView.addSubview(iconView)
        self.contentView.addSubview(titleView)
        self.contentView.addSubview(separatorView)
        self.separatorView.isHidden = false
        self.iconView.isHidden = true
        self.iconView.snp.makeConstraints { (make) in
//            make.centerY.equalTo(self.contentView)
            make.top.equalTo(12)
            make.leading.equalTo(self).offset(9)
        }
        self.titleView.snp.makeConstraints { (make) in
             make.leading.equalTo(self.contentView).offset(15)
            
//            make.centerY.equalTo(self.contentView)
            make.bottom.equalTo(-12)
//            make.centerY.equalTo(self)
//            make.leading.equalTo(self).offset(15)
//            make.trailing.equalTo(-(KscreenWidth-90))
//            make.height.equalTo(25)
            make.width.equalTo(80)
        }
//        self.separatorView.snp.makeConstraints { (make) in
//            make.left.equalTo(<#T##other: CGFloat##CGFloat#>)
//        }
    }
    func getFont() -> CGFloat {
        return (WMSFonts["founctionCell"]!)[userInfo!.fontLevel.rawValue]
    }
    //MARK: - public
    func clearContent(){
        
    }

}
